var CultureMuncher_WorkflowRunner = {
	init: function(workflow) {
		// init ui
		this.doc = null;
		this.status = $("#status")[0];
		this.progressMeter = $("#progressmeter")[0];
		this.canvas = $('#canvas')[0];

		// init arguments
		if("arguments" in window && window.arguments.length > 0) {
			this.workflow = window.arguments[0].workflow;
			this.setStatus('Running ...');
		} else {
			alert('[workflowrunner.xul] Workflow Initilization Error!');
			this.setStatus('Error ...')
		}

		//dump(printStackTrace());

		// init hidden browser
		this.cmbrowser = document.getElementById("cm-browser");
		if (this.cmbrowser) {
			this.cmbrowser.style.height = "0px";
			this.cmbrowser.webNavigation.allowAuth = true;
			this.cmbrowser.webNavigation.allowImages = false;
			this.cmbrowser.webNavigation.allowJavascript = true;
			this.cmbrowser.webNavigation.allowMetaRedirects = true;
			this.cmbrowser.webNavigation.allowPlugins = false;
			this.cmbrowser.webNavigation.allowSubframes = false;
			this.cmbrowser.addEventListener("DOMContentLoaded", function (e) {
				CultureMuncher_WorkflowRunner.browserLoaded(e);
			}, true);
		}
		// run workflow
		this.refresh();
	},
	browserLoaded: function(e) {
		if ((e.originalTarget.nodeName == '#document') &&
		(e.originalTarget.defaultView.location.href == this.workflow.url)) {
			this.doc = e.originalTarget;
			dump('Loaded: ' + this.doc.location.href + '\n');
			this.run();
		}
	},
	readUrl: function(url) {
		$.ajax({
			url: this.workflow.url,
			success: function(data) {
				document.title = url;
				CultureMuncher_WorkflowRunner.run(data);
			},
			error: function(data) {
				alert('Url \'' + url + '\' failed to load');
			}
		});
	},
	run: function() {
		if (this.doc == null) {
			alert('[workflowrunner.run] Document is null!');
			return;
		}

		// init variables
		this.ids = [];
		$(this.canvas).empty();

		var cols = new Array();

		// start running for all children
		if (this.workflow.children.length > 0) {
			for (var i=0; i<this.workflow.children.length; i++) {
				var step = this.workflow.children[i];
				step.run(this.doc, this.ids, cols);
			}
		}

		//////////////////////////////
		// now append all control
		// get definition

		for (var col in cols) {			
			var def = CultureMuncher_Workflow.TableDefinition.factory(col);
			var tree = document.createElement('tree');
			tree.setAttribute('flex', 1);
			tree.setAttribute('enableColumnDrag', 'true');
			var treecols = document.createElement('treecols');
			var treechildren = document.createElement('treechildren');

			for (var i=0; i<def.columns.length; i++) {
				// column
				var colv = def.columns[i];
				var tcol = document.createElement('treecol');
				tcol.setAttribute('id',colv.name);
				tcol.setAttribute('label', colv.name);
				tcol.setAttribute('flex', 1);
				treecols.appendChild(tcol);
				var splitter = document.createElement('splitter');
				splitter.setAttribute('class', 'tree-splitter');
				treecols.appendChild(splitter);

			}
			tree.appendChild(treecols);

			// row
			for (var i=0; i<cols[col].length; i++) {
				var treeitem = document.createElement('treeitem');
				var treerow = document.createElement('treerow');

				for (var j=0; j<def.columns.length; j++) {
					var treecell = document.createElement('treecell');
					treecell.setAttribute('label', cols[col][i][def.columns[j].name] == null ? '' : cols[col][i][def.columns[j].name]);
					treerow.appendChild(treecell);
				}
				treeitem.appendChild(treerow);
				treechildren.appendChild(treeitem);
			}

			tree.appendChild(treechildren);
			
			// append tree
			this.canvas.appendChild(tree);
			// append splitter
			//var splter = document.createElement('splitter');
			//splter.setAttribute('class', 'tree-splitter');
			//this.canvas.appendChild(splitter);
		}

		this.setStatus('Done ...');
	},
	refresh: function() {
		//this.readUrl(this.workflow.url);
		dump('Loading: ' + this.workflow.url + '\n');
		this.cmbrowser.webNavigation.loadURI(this.workflow.url,
		Components.interfaces.nsIWebNavigation, null, null, null);
	},
	setProgress: function(value) {
		this.progressMeter.value = 100 * value / maxProgress;
	},
	setStatus: function(text) {
		this.status.label = "Status: " + text + "...";
	}
}

window.addEventListener("load", function () {
	CultureMuncher_WorkflowRunner.init();
}, false);